﻿using Ezpay.ServiceContract;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ezpay.UI.ViewModels
{
    class SearchViewModel : NotificationObject
    {
        private string searchText;
        public string SearchText
        {
            get { return searchText; }
            set
            {
                SetSearchTextNoSearch(value);
                OnSearchTextChanged();
            }
        }

        public void SetSearchTextNoSearch(string text)
        {
            searchText = text;
            RaisePropertyChanged(() => SearchText);
        }

        private SearchResultViewModel result;
        public SearchResultViewModel Result
        {
            get { return result; }
            set
            {
                result = value;
                RaisePropertyChanged(() => Result);
            }
        }

        private void OnSearchTextChanged()
        {
            if (string.IsNullOrWhiteSpace(searchText))
            {
                Result = null;
                return;
            }

            Search(searchText);
        }
        private async void Search(string query)
        {
            var response = await Task.Factory.StartNew(() =>
            {
                return _ezpayService.Search(new SearchRequest() { Query = query });
            });

            var items = response.Results.Select(x => new SearchResultItemViewModel(_regionManager, _eventAggregator, x)).ToList();

            Result = new SearchResultViewModel(_eventAggregator, items);
        }

        private Dictionary<string, List<string>> samples;
        private IRegionManager _regionManager;
        private IEventAggregator _eventAggregator;
        private IEzpayService _ezpayService;
        public SearchViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IEzpayService ezpayService)
        {
            _ezpayService = ezpayService;
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            eventAggregator.GetEvent<SearchResultSelectedEvent>().Subscribe(OnResultSelected);
        }

        private void OnResultSelected(SearchResultItemViewModel vm)
        {
            SetSearchTextNoSearch(vm.SelectedItem.Description);
        }

    }

    class SearchResultViewModel : NotificationObject
    {
        private bool hasItems;
        public bool HasItems
        {
            get { return hasItems; }
            set
            {
                hasItems = value;
                RaisePropertyChanged(() => HasItems);
            }
        }

        public ICollection<SearchResultItemViewModel> Items { get; set; }

        public SearchResultViewModel(IEventAggregator eventAggregator, ICollection<SearchResultItemViewModel> items)
        {
            eventAggregator.GetEvent<SearchResultSelectedEvent>().Subscribe(OnResultSelected);

            Items = items;
            HasItems = Items.Count > 0;
        }

        private void OnResultSelected(SearchResultItemViewModel obj)
        {
            HasItems = false;
        }
    }

    class SearchResultSelectedEvent : CompositePresentationEvent<SearchResultItemViewModel>
    {

    }

    class SearchResultItemViewModel : NotificationObject
    {
        private IRegionManager _regionManager;
        private IEventAggregator _eventAggregator;
        private SearchResultGroup _result;
        public SearchResultItemViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, SearchResultGroup result)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _result = result;
        }

        public string Category
        {
            get
            {
                return _result.GroupName;
            }
        }

        public string Icon { get { return string.Format("{0}/{1}.png", "pack://application:,,,/Ezpay.UI;component/Resources/Images", Category); } }
        public List<SearchResult> Items
        {
            get
            {
                return _result.Items.ToList();
            }
        }

        private SearchResult selectedItem;
        public SearchResult SelectedItem
        {
            get { return selectedItem; }
            set
            {
                selectedItem = value;
                if (value != null)
                {
                    var query = new UriQuery();
                    query.Add("ID", value.Identifier);
                    _regionManager.ShowInMain(Category + "ViewModel" + query.ToString());
                    _eventAggregator.GetEvent<SearchResultSelectedEvent>().Publish(this);
                }
            }
        }

    }
}
